module Main where

import IO ( stdin, hGetContents )
import System ( getArgs, getProgName, exitFailure )

import Lexfgrammar
import Parfgrammar
import Printfgrammar
import Absfgrammar

import Deforestation

import ErrM

check :: String -> IO () 
check s = case pProgram (myLexer s) of
            Bad err  -> do putStrLn "SYNTAX ERROR"
                           putStrLn err
                           exitFailure 
            Ok  tree -> do
                putStrLn $ show tree
                putStrLn $ "-----------------"
                p <- deforest tree
                putStrLn $ show p
                putStrLn $ "--------------------------"
                putStrLn $ printTree tree

main :: IO ()
main = do args <- getArgs
          case args of
            [file] -> readFile file >>= check
            _      -> do putStrLn "Usage: deforester <SourceFile>"
                         exitFailure
